package com.example.lenovo.wechat;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton weixin_btn;
    private ImageButton friend_btn;
    private ImageButton cover_btn;
    private ImageButton setting_btn;

    private WeixinFragment weixinFragment = null;
    private FriendFragment friendFragment = null;
    private CoverFragment coverFragment = null;
    private  MyFragment myFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("MainActivity",this.toString());
        setContentView(R.layout.activity_main);

        weixin_btn = findViewById(R.id.winxin_button);
        friend_btn = findViewById(R.id.my_bufriend_button);
        cover_btn = findViewById(R.id.cover_button);
        setting_btn = findViewById(R.id.my_button);

        weixin_btn.setOnClickListener(this);
        friend_btn.setOnClickListener(this);
        cover_btn.setOnClickListener(this);
        setting_btn.setOnClickListener(this);

        selctTab(0);



    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.winxin_button:
                weixin_btn.setImageResource(R.drawable.weixin);
                selctTab(0);
                break;
            case R.id.my_bufriend_button:
                friend_btn.setImageResource(R.drawable.friend);
                selctTab(1);
                break;
            case R.id.cover_button:
                cover_btn.setImageResource(R.drawable.cover);
                selctTab(2);
                break;
            case R.id.my_button:
                setting_btn.setImageResource(R.drawable.setting);
                selctTab(3);
                break;
                
        }
    }

    public  void selctTab(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (weixinFragment != null) {
            transaction.hide(weixinFragment);
        }
        if (friendFragment != null) {
            transaction.hide(friendFragment);
        }
        if (coverFragment != null) {
            transaction.hide(coverFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        switch (index){
            case 0:
                if (weixinFragment == null) {
                   weixinFragment = new WeixinFragment();
                    transaction.add(R.id.ill_framlayout,weixinFragment);

                }else {
                    transaction.show(weixinFragment);
                }
                break;
            case 1:
                if (friendFragment == null) {
                    friendFragment = new FriendFragment();
                    transaction.add(R.id.ill_framlayout,friendFragment);
                }else {
                    transaction.show(friendFragment);
                }
                break;
            case 2:
                if (coverFragment == null) {
                    coverFragment = new CoverFragment();
                    transaction.add(R.id.ill_framlayout,coverFragment);
                }else {
                    transaction.show(coverFragment);
                }
                break;
            case 3:
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.ill_framlayout,myFragment);
                }else {
                    transaction.show(myFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void setTabDefaultColor() {
        weixin_btn.setImageResource(R.drawable.weixin2);
        friend_btn.setImageResource(R.drawable.friend2);
        cover_btn.setImageResource(R.drawable.cover2);
        setting_btn.setImageResource(R.drawable.setting2);
    }
}
